package com.sr.vo.req;

import com.sr.pojo.WMemberLevel;
import com.sr.util.ToolUtil;
import com.sr.vo.BaseForm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author xiaochi
 * @date 2022/4/30 12:55
 * @desc MemberLevelReqVo
 */
@ApiModel("会员等级实体")
@Data
@EqualsAndHashCode(callSuper = true)
public class MemberLevelReqVo extends BaseForm<WMemberLevel> {

    @ApiModelProperty("会员id更新时必须")
    @Size(min = 19,max = 19,message = "id不正确",groups = Update.class)
    @NotBlank(message = "会员等级id不能为空",groups = Update.class)
    private String id;

    @ApiModelProperty("会员等级名称不能超过32 位")
    @NotBlank(message = "会员等级名称不能为空",groups = {Create.class,Update.class})
    @Length(min = 1,max = 32,message = "会员等级名称长度为 1 ~ 32 位",groups = {Create.class,Update.class})
    private String name;

    @ApiModelProperty("会员等级权限：如0、16、32....")
    @NotBlank(message = "会员等级权限值不能为空")
    @PositiveOrZero(message = "会员等级权限：如0、16、32....",groups = {Create.class,Update.class})
    private Integer scope;

    @Override
    public WMemberLevel buildEntity() {
        WMemberLevel level = ToolUtil.copy(this, WMemberLevel.class);
        Date date = new Date();
        if (StringUtils.isEmpty(level.getId())){
            level.setId(ToolUtil.nextId());
            level.setCreateTime(date);
            level.setUpdateTime(date);
        }else {
            level.setUpdateTime(date);
        }
        return level;
    }
}
